/**
 * @file
 * @brief This file implements a "very" simple sample client.
 * 
 * The client connects to the server, running at SERVERHOST:SERVERPORT
 * and performs a number of storage_* operations. If there are errors,
 * the client exists.
 */

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "storage.h"
#include "utils.h" #hahaha 

void menu();//prints menu on terminal

FILE* client=NULL;

/**
 * @brief Start a client to interact with the storage server.
 *
 * If connect is successful, the client performs a storage_set/get() on
 * TABLE and KEY and outputs the results on stdout. Finally, it exists
 * after disconnecting from the server.
 */
int main(int argc, char *argv[]) {

	char str[256];

	void* conn;

	int status;

	int logging;
	logging = LOGGING;

	struct storage_record r;

	/*all the strings below are one character longer than the correct 		number to test if the user has keyed in an input that is too long
	*/
	char server_host[65];
	char server_port[9];
	int port_int;

	char server_username[65];
	char server_password[65];

	char key[21];
	char table[21];
	char value[801];

	menu();

	printf(">Please enter your selection:");

	//reads current time and convert to local time
	time_t rawtime;
	struct tm* timeinfo;
	char current[100];
	char month[10];
	char day[10];
	char hour[10];
	char minute [10];
	char second[10];

	time(&rawtime);
	timeinfo = localtime(&rawtime);
	if(((timeinfo->tm_mon)+1)<10)
		sprintf(month,"0%d",((timeinfo->tm_mon)+1));
	else
		sprintf(month,"%d",((timeinfo->tm_mon)+1));

	if((timeinfo->tm_mday)<10)
		sprintf(day,"0%d",(timeinfo->tm_mday));
	else
		sprintf(day,"%d",(timeinfo->tm_mday));

	if((timeinfo->tm_hour)<10)
		sprintf(hour,"0%d",(timeinfo->tm_hour));
	else
		sprintf(hour,"%d",(timeinfo->tm_hour));	
		
	if((timeinfo->tm_min)<10)
		sprintf(minute,"0%d",(timeinfo->tm_min));
	else 
		sprintf(minute,"%d",(timeinfo->tm_min));
		
	if((timeinfo->tm_sec)<10)
		sprintf(second,"0%d",(timeinfo->tm_sec));
	else
		sprintf(second,"%d",(timeinfo->tm_sec));

	sprintf (current, "Client-%d-%s-%s-%s-%s-%s.log",((timeinfo->tm_year)+1900),month,day,hour,minute,second);

	if(logging==2)	
		client=fopen(current,"a");

	fgets(str,257,stdin);

	while(str!=NULL){
		//parses user inputs
		//first case, connect
		if((strcmp(str,"CONNECT\n")==0)||(strcmp(str,"Connect\n")==0)||
				(strcmp(str,"connect\n")==0)||(strcmp(str,"1\n")==0)||
				(strcmp(str,"1.\n")==0)){

			//when the menu is been disappeared from the command-line,
			//prints the menu again

			//reads server_host and server_port from the inputs
			printf(">Please input the hostname:");

			gets(server_host);
			while(strlen(server_host)>64){
				printf("Input is too long, please retype\n");
				printf(">Please input the hostname:");
				gets(server_host);
			}
			
			printf(">Please input the port:");
			gets(server_port);
			while(strlen(server_port)>8){
				printf("Input is too long, please retype\n");
				printf(">Please input the port:");
				gets(server_port);
			}
			
			//after error checking, converts the string back into integer so that it
			//can be used in storage_connect
			port_int=atoi(server_port);
			
			//connects to the server
			printf("Connecting to the local host...\n");				

  			conn = storage_connect(server_host, port_int);
  			if(!conn) {
   				 printf("Cannot connect to server @ %s:%d. Error code: %d.\n",server_host, port_int, errno);
   			 	return -1;
  			}
			printf("storage_auth: successful.\n");
			
			//gets ready for next commands from the user
			printf(">Please enter your selection:");
			fgets(str,257,stdin);
		}
		//second case, authenticate
		else if((strcmp(str,"AUTHENTICATE\n")==0)||(strcmp(str,"Authenticate\n")==0)||
				(strcmp(str,"authenticate\n")==0)||(strcmp(str,"2\n")==0)||
				(strcmp(str,"2.\n")==0)){
		
			//reads server_username and server_password from the inputs
			printf(">Please input the username:");

			//tests the length of the input(shouldn't exceed 64)
			gets(server_username);
			while(strlen(server_username)>64){
				printf("Input is too long, please re-enter\n");
				printf(">Please input the username:");
				gets(server_username);
			}
			
			printf(">Please input the password:");
			//tests the length of the input(shouldn't exceed 64)
			gets(server_password);
			while(strlen(server_password)>64){
				printf("Input is too long, please re-enter\n");
				printf(">Please input the password:");
				gets(server_password);
			}

 			 // Authenticate the client.
  			status = storage_auth(server_username,server_password, conn);
  			if(status != 0) {
    				printf("storage_auth failed with username '%s' and password '%s'. " \
				       "Error code: %d.\n",server_username,server_password, errno);
    				storage_disconnect(conn);
   				 return status;
 			 }
  			printf("storage_auth: successful.\n");

			//gets ready for next commands from the user
			printf(">Please enter your selection:");
			fgets(str,257,stdin);
		}
		//third case, get
		else if((strcmp(str,"GET\n")==0)||(strcmp(str,"Get\n")==0)||
				(strcmp(str,"get\n")==0)||(strcmp(str,"3\n")==0)||
				(strcmp(str,"3.\n")==0)){
			//reads table and key from the inputs
			printf(">Please input the table:");

			//tests the length of the input(shouldn't exceed 64)
			gets(table);
			while(strlen(table)>20){
				printf("Input is too long, please re-enter\n");
				printf(">Please input the table:");
				gets(table);
			}
			
			printf(">Please input the key:");

			//tests the length of the input(shouldn't exceed 64)
			gets(key);
			while(strlen(key)>21){
				printf("Input is too long, please re-enter\n");
				printf(">Please input the key:");
				gets(key);
			}
			
			// Issue storage_get
 			status = storage_get(table, key, &r, conn);
  			if(status != 0) {
    				printf("storage_get failed. Error code: %d.\n", errno);
    				storage_disconnect(conn);
    				return status;
  			}
  			printf("storage_get: the value returned for key '%s' is '%s'.\n",key, r.value);
			
			//gets ready for next commands from the user
			printf(">Please enter your selection:");
			fgets(str,257,stdin);
		}
		//fourth case, set
		else if((strcmp(str,"SET\n")==0)||(strcmp(str,"Set\n")==0)||
				(strcmp(str,"set\n")==0)||(strcmp(str,"4\n")==0)||
				(strcmp(str,"4.\n")==0)){

			//reads table, key, and value from the inputs
			printf(">Please input the table:");

			//tests the length of the input(shouldn't exceed 64)
			gets(table);
			while(strlen(table)>20){
				printf("Input is too long, please re-enter\n");
				printf(">Please input the table:");
				gets(table);
			}
			
			
			printf(">Please input the key:");

			//tests the length of the input(shouldn't exceed 64)
			gets(key);
			while(strlen(key)>20){
				printf("Input is too long, please retype\n");
				printf(">Please input the key:");
				gets(key);
			}

			printf(">Please input the value:");

			//tests the length of the input(shouldn't exceed 64)
			gets(value);
			while(strlen(value)>800){
				printf("Input is too long, please re-enter\n");
				printf(">Please input the value:");
				gets(value);
			}

 			// Issue storage_set
  			strncpy(r.value, "some_value", sizeof r.value);
 			status = storage_set(table, key, &r, conn);
  			if(status != 0) {
    				printf("storage_set failed. Error code: %d.\n", errno);
    				storage_disconnect(conn);
   				return status;
  			}
  			printf("storage_set: successful.\n");

			//gets ready for next commands from the user
			printf(">Please enter your selection:");
			fgets(str,257,stdin);
		}
		//fifth case, disconnect
		else if((strcmp(str,"DISCONNECT\n")==0)||(strcmp(str,"Disconnect\n")==0)||
				(strcmp(str,"disconnect\n")==0)||(strcmp(str,"5\n")==0)||
				(strcmp(str,"5.\n")==0)){
			// Disconnect from server
  			status = storage_disconnect(conn);
  			if(status != 0) {
    				printf("storage_disconnect failed. Error code: %d.\n", errno);
    				return status;
  			}
			printf("storage_disconnect:succesful.\n");

			//the user might wants to re-connect
			printf(">Please enter your selection:");
			fgets(str,257,stdin);
		}
		else if((strcmp(str,"EXIT\n")==0)||(strcmp(str,"Exit\n")==0)||
			(strcmp(str,"exit\n")==0)||(strcmp(str,"6\n")==0)||
			(strcmp(str,"6.\n")==0)){
			break;
		}
		else{
			printf("Wrong input, please re-enter\n");
			printf(">Please enter your selection:");

			fgets(str,257,stdin);
		}
	}
	fclose(client);
	client=NULL;
	return 0;
}
 
void menu(){
	printf("1.CONNECT\n");
	printf("2.AUTHENTICATE\n");
	printf("3.GET\n");
	printf("4.SET\n");
	printf("5.DISCONNET\n");
	printf("6.EXIT\n");
	printf("----------------\n");
	return;
}

